
using FGUFW.Core.System;
using UnityEngine;
using System.Collections;
using FGUFW.Core;
using System.Collections.Generic;
using UnityEngine.AddressableAssets;

namespace FGUFW.Play.StepGrid
{
    public class DefaultModeModule : IPlayModule,IDefaultModeInput
    {
        static public class Config
        {
            public const float MAX_SPEED = 1f;
            public const float MIN_SPEED = 0.5f;
            public const string AUDIOCLIP_RES = "Assets/JinJvLi/GamePlay/StepGrid/AudioClip/";
            public const string MAX_NUMBER = "DefaultModeModule.MaxNumber";
        }

        public int SuccCount
        {
            get;
            private set;
        }

        public float Speed
        {
            get;
            private set;
        }

        GameObject _view;
        public byte[] Grids;

        Coroutine _moveLoop;
        Dictionary<string,AudioClip> _clickAudioDict = new Dictionary<string, AudioClip>();

        public void Init()
        {
            if(Grids==null)
            {
                Grids = new byte[314];
                for (int i = 0; i < Grids.Length; i++)
                {
                    Grids[i]=(byte)Random.Range(0,4);
                }
            }

            if(_view==null)
            {
                StepGridPlayManager.I.UISystem.Open("DefaultMode",loadUICompleted);
            }
            
            if(_clickAudioDict.Count==0)
            {
                for (int i = 0; i < 4; i++)
                {
                    Addressables.LoadAssetAsync<AudioClip>(Config.AUDIOCLIP_RES+"click_"+i+".mp3").Completed+=(aoh)=>
                    {
                        Debug.Log("音频加载完成 "+aoh.Result.name);
                        _clickAudioDict.Add(aoh.Result.name,aoh.Result);
                    };
                }
            }

            SuccCount=0;
        }

        void loadUICompleted(GameObject view)
        {
            _view = view;
            _view.GetComponent<DefaultModeView>().DefaultModeInput=this;
            Hide();
        }

        public void Hide()
        {
            _view.transform.localPosition = UISystem.Config.HIDE_POS;
        }

        public void Show()
        {
            _view.transform.localPosition = UISystem.Config.SHOW_POS;
            _view.GetComponent<DefaultModeView>().Init();
        }

        public void Release()
        {
            GameObject.DestroyImmediate(_view);
            _view=null;
        }

        public void OnClickGrid(Vector2Int grid)
        {
            int line = grid.x%Grids.Length;
            int num = grid.y;
            if(Grids[line]==num)
            {
                SuccCount++;
            }
            else
            {
                gameEnd();
            } 
            StepGridPlayManager.I.AudioSystem.Play(_clickAudioDict["click_"+num]);
        }

        public void OnClickStart()
        {
            _moveLoop = moveLoop().Start();
        }

        void gameEnd()
        {
            _moveLoop?.Stop();
            Speed=0;
            _view.GetComponent<DefaultModeView>().ShowEndBtn();
        }
        
        IEnumerator moveLoop()
        {
            Speed = Config.MIN_SPEED;
            while(Speed<Config.MAX_SPEED)
            {
                Speed+=0.0002f;
                if(Speed>Config.MAX_SPEED)
                {
                    Speed = Config.MAX_SPEED;
                }
                yield return null;
            }
        }

        public void OnLineEnd(int line,bool click)
        {
            if(!click)
            {
                gameEnd();
            }
        }

        public void OnClickEnd()
        {
            if(PlayerPrefs.GetInt(DefaultModeModule.Config.MAX_NUMBER,0)<SuccCount)
            {
                PlayerPrefs.SetInt(DefaultModeModule.Config.MAX_NUMBER,SuccCount);
            }
            Release();
            StepGridPlayManager.I.Module<HomeModule>().Show();
        }

    }
}